﻿using System;
using System.Text.RegularExpressions;
using System.Web;

namespace Com.Core.Helps
{
    /// <summary>
    /// cookies帮助类，此方法全局使用aes加密算法
    /// </summary>
    public class CookiesHelper
    {
        /// <summary>
        /// 设置cookies，此cookies随浏览器关闭而失效
        /// </summary>
        /// <param name="cookiesName"></param>
        /// <param name="cookiesValue"></param>
        public static void SetCookies(string cookiesName, string cookiesValue)
        {
            var httpCookie = HttpContext.Current.Response.Cookies[cookiesName] ?? new HttpCookie(cookiesName);
            httpCookie.Value = EncryptHelper.AES.Encrypt(cookiesValue);
        }
        /// <summary>
        /// 读取cookies
        /// </summary>
        /// <param name="cookiesName">cookies名称</param>
        /// <returns></returns>
        public static string ReadCookies(string cookiesName)
        {
            string temp = string.Empty;
            if (HttpContext.Current.Request.Cookies[cookiesName] != null)
            {
                temp = HttpContext.Current.Request.Cookies[cookiesName].Value;
                temp = EncryptHelper.AES.Decrypt(temp);
            }
            return temp;
        }

        /// <summary>
        /// 清除Cookies
        /// </summary>
        /// <param name="cookiesName"></param>
        public static void ClearCookies(string cookiesName)
        {
            var httpCookie = HttpContext.Current.Response.Cookies[cookiesName];
            if (httpCookie != null)
                httpCookie.Value = null;
        }

        /// <summary>
        /// 将指定cookies设为过期
        /// </summary>
        /// <param name="cookiesName">cookies名称</param>
        public static void RemoveCookies(string cookiesName)
        {
            var httpCookie = HttpContext.Current.Response.Cookies[cookiesName];
            if (httpCookie != null)
                httpCookie.Value = EncryptHelper.AES.Encrypt("过期");
            var cookie = HttpContext.Current.Response.Cookies[cookiesName];
            if (cookie != null)
                cookie.Expires = DateTime.Now.AddDays(-1);
        }

        /// <summary>
        /// 二次加密cookies，首先在开头添加5-20位的随机数，然后添加真实cookies，末尾再次添加5-20位随机数，中间用@@??隔开
        /// </summary>
        /// <param name="cookiesName"></param>
        /// <param name="cookiesValue"></param>
        public static void SetEncryptCookies(string cookiesName,string cookiesValue)
        {
            cookiesValue=RandomHelper.CreateRandom(5,20,RandomCharType.Charaters)+"@_@"+cookiesValue+ "@_@" + RandomHelper.CreateRandom(5, 20, RandomCharType.Charaters);
            SetCookies(cookiesName, cookiesValue);
        }

        public static string ReadEncryptCookies(string cookiesName)
        {
            string cookies = ReadCookies(cookiesName);
            try
            {
                var temp = Regex.Split(cookies, "@_@", RegexOptions.IgnoreCase);
                return temp[1];
            }
            catch
            {
                return "";
            }
        }
    }
}
